#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

const int maxN = 201;

static int edge[maxN][maxN];
bool visited[maxN];
int father[maxN];
int N, M; // M is vertex, N is edge.
int maxflow;

void Ford_Fulerson()
{
	while (1) {
		queue<int> q;
		memset(visited, 0, sizeof(visited));
		memset(father, 0, sizeof(father));
		int now;

		visited[0] = true;
		q.push(0);
		// use bfs to find a augment path.		
		while (!q.empty()) {
			now = q.front();
			q.pop();
			if (now == M-1) 
				break;
			for (int i = 0; i < M; i++) {
				if (edge[now][i] && !visited[i]) {
					father[i] = now;
					visited[i] = true;
					q.push(i);
				}	
			}
		}/// while~

		if (!visited[M-1])
			break;
		int u, min = 0xFFFF;

		for (u = M-1; u; u = father[u]) {
			if (edge[father[u]][u] < min) 
				min = edge[father[u]][u];
		}

		for (u = M-1; u; u = father[u]) {
			edge[father[u]][u] -= min; // forward arch subtract.
			edge[u][father[u]] += min; // backword arch add.
		}

		maxflow += min;
	}/// while~
}

int main()
{
	int s, e, w;
	while (cin >> N >> M) {
		maxflow = 0;
		memset(edge, 0, sizeof(edge));
		for (int i = 0; i < N; i++) {
			cin >> s >> e >> w;
			edge[s-1][e-1] += w;
		}

		Ford_Fulerson();
		cout << maxflow << endl;
	}

	return 0;
}
